package com.yz.design.settlement;

import lombok.Data;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * @ClassName SettlementContext
 * @Description 标准结算上下文
 * @Author 广长
 * @Date 2020/7/10
 * @Version V1.0
 **/
@Data
@ToString
public class SettlementContext {
    private static Logger logger = LoggerFactory.getLogger("logs");
    /**
     * 结算策略
     */
    private SettlementStrategy settlementStrategy;

    /**
     * 结算id
     */
    private Integer settlementId;

    /**
     * 结算日期
     */
    private Date paymentDate;
    /**
     * 备注
     */
    private String remark;
    /**
     * 保存状态(0.保存草稿, 1.结算)
     */
    private String settlementStatus;
    /**
     * 操作用户id
     */
    private Integer userId;

    /**
     * 已存在结算明细清单
     */
    private List<Object> settlementDetailedList;

    /**
     * 现在结算明细清单
     */
    private List<Object> nowSettlementDetailedList;

    /**
     * 付款上下文对象
     */
    private CitySiteSettlementContext payContext;

    /**
     * 收款上下文对象
     */
    private CitySiteSettlementContext receiveContext;

    public SettlementContext(Integer settlementId, String settlementStatus, SettlementStrategy settlementStrategy) {
        this.settlementStrategy = settlementStrategy;
        this.settlementStatus = settlementStatus;
        if (settlementId == null && "0".equals(settlementStatus)) {
            // 新增
            this.settlementId = settlementId;
        } else if (settlementId != null && "0".equals(settlementStatus)) {
            // 修改/删除 草稿
            this.settlementId = settlementId;
            this.settlementDetailedList = this.settlementStrategy.getSettlementDetailedList(this);
        } else if (settlementId != null && "1".equals(settlementStatus)) {
            // 修改/撤销 结算
            this.settlementId = settlementId;
            this.payContext = this.settlementStrategy.getPayContext(this);
            this.receiveContext = this.settlementStrategy.getReceiveContext(this);
            this.settlementDetailedList = this.settlementStrategy.getSettlementDetailedList(this);
        }
    }

    public SettlementContext(SettlementStrategy settlementStrategy) {
        this.settlementStrategy = settlementStrategy;
    }

    public SettlementContext saveDraft() {
        logger.debug("保存草稿");
        return settlementStrategy.saveDraft(this);
    }

    public SettlementContext removeDraft() {
        logger.debug("删除草稿");
        settlementStrategy.removeDraft(this);
        return this;
    }
}
